package com.hyz.service.impl;

import client.AssetClient;
import client.LogClient;
import com.alibaba.fastjson.JSON;
import com.hyz.pojo.ResponseResult;
import com.hyz.pojo.assetManager.GetAssetImportsByAssetManagerIdResponse;
import com.hyz.pojo.assetManager.InsertAssetImportRequest;
import com.hyz.pojo.assetManager.UnassignedAssetResponse;
import com.hyz.pojo.assetSecondment.AssetSecondmentResponse;
import com.hyz.service.AssetManagerService;
import com.hyz.util.Conversion;
import com.hyz.util.GetValue;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pojo.asset.Asset;
import pojo.asset.Repository;
import pojo.log.assetImport.AssetImport;
import pojo.log.assetImport.AssetImportResultEnum;
import pojo.log.assetLog.AssetLog;
import pojo.log.IsComplete;
import pojo.log.assetSecondment.AssetSecondment;
import pojo.log.assetSecondment.AssetSecondmentResultEnum;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author HGD
 * @date 2022/10/3 21:25
 */
@Service
public class AssetManagerServiceImpl implements AssetManagerService {
    @Autowired
    private AssetClient assetClient;
    @Autowired
    private GetValue getValue;
    @Resource
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private LogClient logClient;
    @Autowired
    private Conversion conversion;


    @Override
    public ResponseResult<List<UnassignedAssetResponse>> getUnassignedAsset() {
        List<Asset> assets = assetClient.getUnassignedAssets();
        List<UnassignedAssetResponse> responses = new ArrayList<>();
        for (Asset asset : assets) {
            UnassignedAssetResponse response = new UnassignedAssetResponse();
            response.setId(asset.getId());
            response.setAssetId(asset.getAssetId());
            response.setAssetName(asset.getAssetName());
            response.setUnit(asset.getUnit());
            response.setBuyDate(asset.getBuyDate());
            response.setSupplier(asset.getSupplier());
            response.setLife(asset.getLife());
            response.setStatus(asset.getStatus().getValue());
            responses.add(response);
        }
        return new ResponseResult<>(200, "成功访问", responses);
    }

    @Override
    public ResponseResult<List<Repository>> getAllRepositories() {
        List<Repository> repositories = assetClient.getAllRepositories();
        return new ResponseResult<>(200, "成功", repositories);
    }

    @Override
    public ResponseResult<String> batchInsertAssetImports(InsertAssetImportRequest request) {
        List<Long> assetIds = request.getAssetIds();
        Long repositoryId = request.getRepositoryId();
        if (assetIds == null || repositoryId == null) {
            return new ResponseResult<>(500, "参数错误");
        }
        // TODO: 获取用户id
        Long assetManagerId = 1L;
        // TODO: 获取用户职责
        String right = "assetManager";
        // 1. 入库申请创建
        List<AssetImport> assetImports = new ArrayList<>();
        // 2. 资产日志创建
        List<AssetLog> assetLogs = new ArrayList<>();
        for (Long assetId : assetIds) {
            AssetImport assetImport = new AssetImport();
            assetImport.setId(getValue.getUuid());
            assetImport.setAssetId(assetId);
            assetImport.setRepositoryId(repositoryId);
            assetImport.setAssetManagerId(assetManagerId);
            assetImport.setResult(AssetImportResultEnum.CREATE);
            assetImport.setIsComplete(IsComplete.UNFINISHED);
            assetImports.add(assetImport);

            AssetLog assetLog = new AssetLog();
            assetLog.setId(getValue.getUuid());
            assetLog.setAssetId(assetId);
            assetLog.setResult(AssetImportResultEnum.CREATE.getValue());
            assetLogs.add(assetLog);
        }

        String assetImportsJson = JSON.toJSONString(assetImports);
        String assetLogsJson = JSON.toJSONString(assetLogs);

        rabbitTemplate.convertAndSend(right, "batchInsertAssetImports", assetImportsJson);
        rabbitTemplate.convertAndSend("batchInsertAssetLogs", null, assetLogsJson);
        return new ResponseResult<>(200, "成功");
    }

    @Override
    public ResponseResult<List<GetAssetImportsByAssetManagerIdResponse>> getAssetImportsByAssetManagerId() {
        List<GetAssetImportsByAssetManagerIdResponse> responses = new ArrayList<>();
        // TODO: 获取用户id
        Long assetManagerId = 1L;

        List<AssetImport> assetImports = logClient.getAssetImportsByAssetManagerId(assetManagerId);
        for (AssetImport assetImport : assetImports) {
            GetAssetImportsByAssetManagerIdResponse response = new GetAssetImportsByAssetManagerIdResponse();
            response.setAssetId(assetImport.getAssetId());
            Asset asset = assetClient.getAssetById(assetImport.getAssetId());
            response.setAssetName(asset.getAssetName());
            response.setTime(assetImport.getCreateTime().toString());
            response.setStatus(assetImport.getResult().getValue());
            response.setIsCompleted(assetImport.getIsComplete().getValue());
            responses.add(response);
        }
        return new ResponseResult<>(200, "成功", responses);
    }

    @Override
    public ResponseResult<List<AssetSecondmentResponse>> amGetAssetSecondments() {
        List<AssetSecondment> assetSecondments = new ArrayList<>();

        assetSecondments.addAll(
                logClient.getAssetSecondments(AssetSecondment.builder()
                        .isComplete(IsComplete.UNFINISHED)
                        .result(AssetSecondmentResultEnum.APPROVAL_AGREE)
                        .build()));
        assetSecondments.addAll(
                logClient.getAssetSecondments(AssetSecondment.builder()
                        .isComplete(IsComplete.UNFINISHED)
                        .result(AssetSecondmentResultEnum.FINANCIAL_AGREE)
                        .build()));


        List<AssetSecondmentResponse> responses = conversion.getAssetSecondmentResponse(assetSecondments);
        return new ResponseResult<>(200, "成功", responses);
    }

}
